slang: No need to purify source text for tokeniser.
[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 (DEBUG_PROG) {
926 printf("KIL if (%g %g %g %g) <= 0.0\n",
927 a[0], a[1], a[2], a[3]);
928 }
929
930 if (a[0] < 0.0F || a[1] < 0.0F || a[2] < 0.0F || a[3] < 0.0F) {
931 return GL_FALSE;
932 }
933 }
934 break;
935 case OPCODE_LG2: /* log base 2 */
936 {
937 GLfloat a[4], result[4], val;
938 fetch_vector1(&inst->SrcReg[0], machine, a);
939 /* The fast LOG2 macro doesn't meet the precision requirements.
940 */
941 if (a[0] == 0.0F) {
942 val = 0.0F;
943 }
944 else {
945 val = log(a[0]) * 1.442695F;
946 }
947 result[0] = result[1] = result[2] = result[3] = val;
948 store_vector4(inst, machine, result);
949 }
950 break;
951 case OPCODE_LIT:
952 {
953 const GLfloat epsilon = 1.0F / 256.0F; /* from NV VP spec */
954 GLfloat a[4], result[4];
955 fetch_vector4(&inst->SrcReg[0], machine, a);
956 a[0] = MAX2(a[0], 0.0F);
957 a[1] = MAX2(a[1], 0.0F);
958 /* XXX ARB version clamps a[3], NV version doesn't */
959 a[3] = CLAMP(a[3], -(128.0F - epsilon), (128.0F - epsilon));
960 result[0] = 1.0F;
961 result[1] = a[0];
962 /* XXX we could probably just use pow() here */
963 if (a[0] > 0.0F) {
964 if (a[1] == 0.0 && a[3] == 0.0)
965 result[2] = 1.0;
966 else
967 result[2] = (GLfloat) _mesa_pow(a[1], a[3]);
968 }
969 else {
970 result[2] = 0.0;
971 }
972 result[3] = 1.0F;
973 store_vector4(inst, machine, result);
974 if (DEBUG_PROG) {
975 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
976 result[0], result[1], result[2], result[3],
977 a[0], a[1], a[2], a[3]);
978 }
979 }
980 break;
981 case OPCODE_LOG:
982 {
983 GLfloat t[4], q[4], abs_t0;
984 fetch_vector1(&inst->SrcReg[0], machine, t);
985 abs_t0 = FABSF(t[0]);
986 if (abs_t0 != 0.0F) {
987 /* Since we really can't handle infinite values on VMS
988 * like other OSes we'll use __MAXFLOAT to represent
989 * infinity. This may need some tweaking.
990 */
991 #ifdef VMS
992 if (abs_t0 == __MAXFLOAT)
993 #else
994 if (IS_INF_OR_NAN(abs_t0))
995 #endif
996 {
997 SET_POS_INFINITY(q[0]);
998 q[1] = 1.0F;
999 SET_POS_INFINITY(q[2]);
1000 }
1001 else {
1002 int exponent;
1003 GLfloat mantissa = FREXPF(t[0], &exponent);
1004 q[0] = (GLfloat) (exponent - 1);
1005 q[1] = (GLfloat) (2.0 * mantissa); /* map [.5, 1) -> [1, 2) */
1006
1007 /* The fast LOG2 macro doesn't meet the precision
1008 * requirements.
1009 */
1010 q[2] = (log(t[0]) * 1.442695F);
1011 }
1012 }
1013 else {
1014 SET_NEG_INFINITY(q[0]);
1015 q[1] = 1.0F;
1016 SET_NEG_INFINITY(q[2]);
1017 }
1018 q[3] = 1.0;
1019 store_vector4(inst, machine, q);
1020 }
1021 break;
1022 case OPCODE_LRP:
1023 {
1024 GLfloat a[4], b[4], c[4], result[4];
1025 fetch_vector4(&inst->SrcReg[0], machine, a);
1026 fetch_vector4(&inst->SrcReg[1], machine, b);
1027 fetch_vector4(&inst->SrcReg[2], machine, c);
1028 result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0];
1029 result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1];
1030 result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
1031 result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
1032 store_vector4(inst, machine, result);
1033 if (DEBUG_PROG) {
1034 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1035 "(%g %g %g %g), (%g %g %g %g)\n",
1036 result[0], result[1], result[2], result[3],
1037 a[0], a[1], a[2], a[3],
1038 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
1039 }
1040 }
1041 break;
1042 case OPCODE_MAD:
1043 {
1044 GLfloat a[4], b[4], c[4], result[4];
1045 fetch_vector4(&inst->SrcReg[0], machine, a);
1046 fetch_vector4(&inst->SrcReg[1], machine, b);
1047 fetch_vector4(&inst->SrcReg[2], machine, c);
1048 result[0] = a[0] * b[0] + c[0];
1049 result[1] = a[1] * b[1] + c[1];
1050 result[2] = a[2] * b[2] + c[2];
1051 result[3] = a[3] * b[3] + c[3];
1052 store_vector4(inst, machine, result);
1053 if (DEBUG_PROG) {
1054 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1055 "(%g %g %g %g) + (%g %g %g %g)\n",
1056 result[0], result[1], result[2], result[3],
1057 a[0], a[1], a[2], a[3],
1058 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
1059 }
1060 }
1061 break;
1062 case OPCODE_MAX:
1063 {
1064 GLfloat a[4], b[4], result[4];
1065 fetch_vector4(&inst->SrcReg[0], machine, a);
1066 fetch_vector4(&inst->SrcReg[1], machine, b);
1067 result[0] = MAX2(a[0], b[0]);
1068 result[1] = MAX2(a[1], b[1]);
1069 result[2] = MAX2(a[2], b[2]);
1070 result[3] = MAX2(a[3], b[3]);
1071 store_vector4(inst, machine, result);
1072 if (DEBUG_PROG) {
1073 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1074 result[0], result[1], result[2], result[3],
1075 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
1076 }
1077 }
1078 break;
1079 case OPCODE_MIN:
1080 {
1081 GLfloat a[4], b[4], result[4];
1082 fetch_vector4(&inst->SrcReg[0], machine, a);
1083 fetch_vector4(&inst->SrcReg[1], machine, b);
1084 result[0] = MIN2(a[0], b[0]);
1085 result[1] = MIN2(a[1], b[1]);
1086 result[2] = MIN2(a[2], b[2]);
1087 result[3] = MIN2(a[3], b[3]);
1088 store_vector4(inst, machine, result);
1089 }
1090 break;
1091 case OPCODE_MOV:
1092 {
1093 GLfloat result[4];
1094 fetch_vector4(&inst->SrcReg[0], machine, result);
1095 store_vector4(inst, machine, result);
1096 if (DEBUG_PROG) {
1097 printf("MOV (%g %g %g %g)\n",
1098 result[0], result[1], result[2], result[3]);
1099 }
1100 }
1101 break;
1102 case OPCODE_MUL:
1103 {
1104 GLfloat a[4], b[4], result[4];
1105 fetch_vector4(&inst->SrcReg[0], machine, a);
1106 fetch_vector4(&inst->SrcReg[1], machine, b);
1107 result[0] = a[0] * b[0];
1108 result[1] = a[1] * b[1];
1109 result[2] = a[2] * b[2];
1110 result[3] = a[3] * b[3];
1111 store_vector4(inst, machine, result);
1112 if (DEBUG_PROG) {
1113 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1114 result[0], result[1], result[2], result[3],
1115 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
1116 }
1117 }
1118 break;
1119 case OPCODE_NOISE1:
1120 {
1121 GLfloat a[4], result[4];
1122 fetch_vector1(&inst->SrcReg[0], machine, a);
1123 result[0] =
1124 result[1] =
1125 result[2] =
1126 result[3] = _mesa_noise1(a[0]);
1127 store_vector4(inst, machine, result);
1128 }
1129 break;
1130 case OPCODE_NOISE2:
1131 {
1132 GLfloat a[4], result[4];
1133 fetch_vector4(&inst->SrcReg[0], machine, a);
1134 result[0] =
1135 result[1] =
1136 result[2] = result[3] = _mesa_noise2(a[0], a[1]);
1137 store_vector4(inst, machine, result);
1138 }
1139 break;
1140 case OPCODE_NOISE3:
1141 {
1142 GLfloat a[4], result[4];
1143 fetch_vector4(&inst->SrcReg[0], machine, a);
1144 result[0] =
1145 result[1] =
1146 result[2] =
1147 result[3] = _mesa_noise3(a[0], a[1], a[2]);
1148 store_vector4(inst, machine, result);
1149 }
1150 break;
1151 case OPCODE_NOISE4:
1152 {
1153 GLfloat a[4], result[4];
1154 fetch_vector4(&inst->SrcReg[0], machine, a);
1155 result[0] =
1156 result[1] =
1157 result[2] =
1158 result[3] = _mesa_noise4(a[0], a[1], a[2], a[3]);
1159 store_vector4(inst, machine, result);
1160 }
1161 break;
1162 case OPCODE_NOP:
1163 break;
1164 case OPCODE_NOT: /* bitwise NOT */
1165 {
1166 GLuint a[4], result[4];
1167 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1168 result[0] = ~a[0];
1169 result[1] = ~a[1];
1170 result[2] = ~a[2];
1171 result[3] = ~a[3];
1172 store_vector4ui(inst, machine, result);
1173 }
1174 break;
1175 case OPCODE_NRM3: /* 3-component normalization */
1176 {
1177 GLfloat a[4], result[4];
1178 GLfloat tmp;
1179 fetch_vector4(&inst->SrcReg[0], machine, a);
1180 tmp = a[0] * a[0] + a[1] * a[1] + a[2] * a[2];
1181 if (tmp != 0.0F)
1182 tmp = INV_SQRTF(tmp);
1183 result[0] = tmp * a[0];
1184 result[1] = tmp * a[1];
1185 result[2] = tmp * a[2];
1186 result[3] = 0.0; /* undefined, but prevent valgrind warnings */
1187 store_vector4(inst, machine, result);
1188 }
1189 break;
1190 case OPCODE_NRM4: /* 4-component normalization */
1191 {
1192 GLfloat a[4], result[4];
1193 GLfloat tmp;
1194 fetch_vector4(&inst->SrcReg[0], machine, a);
1195 tmp = a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3];
1196 if (tmp != 0.0F)
1197 tmp = INV_SQRTF(tmp);
1198 result[0] = tmp * a[0];
1199 result[1] = tmp * a[1];
1200 result[2] = tmp * a[2];
1201 result[3] = tmp * a[3];
1202 store_vector4(inst, machine, result);
1203 }
1204 break;
1205 case OPCODE_OR: /* bitwise OR */
1206 {
1207 GLuint a[4], b[4], result[4];
1208 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1209 fetch_vector4ui(&inst->SrcReg[1], machine, b);
1210 result[0] = a[0] | b[0];
1211 result[1] = a[1] | b[1];
1212 result[2] = a[2] | b[2];
1213 result[3] = a[3] | b[3];
1214 store_vector4ui(inst, machine, result);
1215 }
1216 break;
1217 case OPCODE_PK2H: /* pack two 16-bit floats in one 32-bit float */
1218 {
1219 GLfloat a[4];
1220 GLuint result[4];
1221 GLhalfNV hx, hy;
1222 fetch_vector4(&inst->SrcReg[0], machine, a);
1223 hx = _mesa_float_to_half(a[0]);
1224 hy = _mesa_float_to_half(a[1]);
1225 result[0] =
1226 result[1] =
1227 result[2] =
1228 result[3] = hx | (hy << 16);
1229 store_vector4ui(inst, machine, result);
1230 }
1231 break;
1232 case OPCODE_PK2US: /* pack two GLushorts into one 32-bit float */
1233 {
1234 GLfloat a[4];
1235 GLuint result[4], usx, usy;
1236 fetch_vector4(&inst->SrcReg[0], machine, a);
1237 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1238 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1239 usx = IROUND(a[0] * 65535.0F);
1240 usy = IROUND(a[1] * 65535.0F);
1241 result[0] =
1242 result[1] =
1243 result[2] =
1244 result[3] = usx | (usy << 16);
1245 store_vector4ui(inst, machine, result);
1246 }
1247 break;
1248 case OPCODE_PK4B: /* pack four GLbytes into one 32-bit float */
1249 {
1250 GLfloat a[4];
1251 GLuint result[4], ubx, uby, ubz, ubw;
1252 fetch_vector4(&inst->SrcReg[0], machine, a);
1253 a[0] = CLAMP(a[0], -128.0F / 127.0F, 1.0F);
1254 a[1] = CLAMP(a[1], -128.0F / 127.0F, 1.0F);
1255 a[2] = CLAMP(a[2], -128.0F / 127.0F, 1.0F);
1256 a[3] = CLAMP(a[3], -128.0F / 127.0F, 1.0F);
1257 ubx = IROUND(127.0F * a[0] + 128.0F);
1258 uby = IROUND(127.0F * a[1] + 128.0F);
1259 ubz = IROUND(127.0F * a[2] + 128.0F);
1260 ubw = IROUND(127.0F * a[3] + 128.0F);
1261 result[0] =
1262 result[1] =
1263 result[2] =
1264 result[3] = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1265 store_vector4ui(inst, machine, result);
1266 }
1267 break;
1268 case OPCODE_PK4UB: /* pack four GLubytes into one 32-bit float */
1269 {
1270 GLfloat a[4];
1271 GLuint result[4], ubx, uby, ubz, ubw;
1272 fetch_vector4(&inst->SrcReg[0], machine, a);
1273 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1274 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1275 a[2] = CLAMP(a[2], 0.0F, 1.0F);
1276 a[3] = CLAMP(a[3], 0.0F, 1.0F);
1277 ubx = IROUND(255.0F * a[0]);
1278 uby = IROUND(255.0F * a[1]);
1279 ubz = IROUND(255.0F * a[2]);
1280 ubw = IROUND(255.0F * a[3]);
1281 result[0] =
1282 result[1] =
1283 result[2] =
1284 result[3] = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1285 store_vector4ui(inst, machine, result);
1286 }
1287 break;
1288 case OPCODE_POW:
1289 {
1290 GLfloat a[4], b[4], result[4];
1291 fetch_vector1(&inst->SrcReg[0], machine, a);
1292 fetch_vector1(&inst->SrcReg[1], machine, b);
1293 result[0] = result[1] = result[2] = result[3]
1294 = (GLfloat) _mesa_pow(a[0], b[0]);
1295 store_vector4(inst, machine, result);
1296 }
1297 break;
1298 case OPCODE_RCP:
1299 {
1300 GLfloat a[4], result[4];
1301 fetch_vector1(&inst->SrcReg[0], machine, a);
1302 if (DEBUG_PROG) {
1303 if (a[0] == 0)
1304 printf("RCP(0)\n");
1305 else if (IS_INF_OR_NAN(a[0]))
1306 printf("RCP(inf)\n");
1307 }
1308 result[0] = result[1] = result[2] = result[3] = 1.0F / a[0];
1309 store_vector4(inst, machine, result);
1310 }
1311 break;
1312 case OPCODE_RET: /* return from subroutine (conditional) */
1313 if (eval_condition(machine, inst)) {
1314 if (machine->StackDepth == 0) {
1315 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
1316 }
1317 /* subtract one because of pc++ in the for loop */
1318 pc = machine->CallStack[--machine->StackDepth] - 1;
1319 }
1320 break;
1321 case OPCODE_RFL: /* reflection vector */
1322 {
1323 GLfloat axis[4], dir[4], result[4], tmpX, tmpW;
1324 fetch_vector4(&inst->SrcReg[0], machine, axis);
1325 fetch_vector4(&inst->SrcReg[1], machine, dir);
1326 tmpW = DOT3(axis, axis);
1327 tmpX = (2.0F * DOT3(axis, dir)) / tmpW;
1328 result[0] = tmpX * axis[0] - dir[0];
1329 result[1] = tmpX * axis[1] - dir[1];
1330 result[2] = tmpX * axis[2] - dir[2];
1331 /* result[3] is never written! XXX enforce in parser! */
1332 store_vector4(inst, machine, result);
1333 }
1334 break;
1335 case OPCODE_RSQ: /* 1 / sqrt() */
1336 {
1337 GLfloat a[4], result[4];
1338 fetch_vector1(&inst->SrcReg[0], machine, a);
1339 a[0] = FABSF(a[0]);
1340 result[0] = result[1] = result[2] = result[3] = INV_SQRTF(a[0]);
1341 store_vector4(inst, machine, result);
1342 if (DEBUG_PROG) {
1343 printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]);
1344 }
1345 }
1346 break;
1347 case OPCODE_SCS: /* sine and cos */
1348 {
1349 GLfloat a[4], result[4];
1350 fetch_vector1(&inst->SrcReg[0], machine, a);
1351 result[0] = (GLfloat) _mesa_cos(a[0]);
1352 result[1] = (GLfloat) _mesa_sin(a[0]);
1353 result[2] = 0.0; /* undefined! */
1354 result[3] = 0.0; /* undefined! */
1355 store_vector4(inst, machine, result);
1356 }
1357 break;
1358 case OPCODE_SEQ: /* set on equal */
1359 {
1360 GLfloat a[4], b[4], result[4];
1361 fetch_vector4(&inst->SrcReg[0], machine, a);
1362 fetch_vector4(&inst->SrcReg[1], machine, b);
1363 result[0] = (a[0] == b[0]) ? 1.0F : 0.0F;
1364 result[1] = (a[1] == b[1]) ? 1.0F : 0.0F;
1365 result[2] = (a[2] == b[2]) ? 1.0F : 0.0F;
1366 result[3] = (a[3] == b[3]) ? 1.0F : 0.0F;
1367 store_vector4(inst, machine, result);
1368 if (DEBUG_PROG) {
1369 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1370 result[0], result[1], result[2], result[3],
1371 a[0], a[1], a[2], a[3],
1372 b[0], b[1], b[2], b[3]);
1373 }
1374 }
1375 break;
1376 case OPCODE_SFL: /* set false, operands ignored */
1377 {
1378 static const GLfloat result[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
1379 store_vector4(inst, machine, result);
1380 }
1381 break;
1382 case OPCODE_SGE: /* set on greater or equal */
1383 {
1384 GLfloat a[4], b[4], result[4];
1385 fetch_vector4(&inst->SrcReg[0], machine, a);
1386 fetch_vector4(&inst->SrcReg[1], machine, b);
1387 result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F;
1388 result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F;
1389 result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F;
1390 result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F;
1391 store_vector4(inst, machine, result);
1392 if (DEBUG_PROG) {
1393 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1394 result[0], result[1], result[2], result[3],
1395 a[0], a[1], a[2], a[3],
1396 b[0], b[1], b[2], b[3]);
1397 }
1398 }
1399 break;
1400 case OPCODE_SGT: /* set on greater */
1401 {
1402 GLfloat a[4], b[4], result[4];
1403 fetch_vector4(&inst->SrcReg[0], machine, a);
1404 fetch_vector4(&inst->SrcReg[1], machine, b);
1405 result[0] = (a[0] > b[0]) ? 1.0F : 0.0F;
1406 result[1] = (a[1] > b[1]) ? 1.0F : 0.0F;
1407 result[2] = (a[2] > b[2]) ? 1.0F : 0.0F;
1408 result[3] = (a[3] > b[3]) ? 1.0F : 0.0F;
1409 store_vector4(inst, machine, result);
1410 if (DEBUG_PROG) {
1411 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1412 result[0], result[1], result[2], result[3],
1413 a[0], a[1], a[2], a[3],
1414 b[0], b[1], b[2], b[3]);
1415 }
1416 }
1417 break;
1418 case OPCODE_SIN:
1419 {
1420 GLfloat a[4], result[4];
1421 fetch_vector1(&inst->SrcReg[0], machine, a);
1422 result[0] = result[1] = result[2] = result[3]
1423 = (GLfloat) _mesa_sin(a[0]);
1424 store_vector4(inst, machine, result);
1425 }
1426 break;
1427 case OPCODE_SLE: /* set on less or equal */
1428 {
1429 GLfloat a[4], b[4], result[4];
1430 fetch_vector4(&inst->SrcReg[0], machine, a);
1431 fetch_vector4(&inst->SrcReg[1], machine, b);
1432 result[0] = (a[0] <= b[0]) ? 1.0F : 0.0F;
1433 result[1] = (a[1] <= b[1]) ? 1.0F : 0.0F;
1434 result[2] = (a[2] <= b[2]) ? 1.0F : 0.0F;
1435 result[3] = (a[3] <= b[3]) ? 1.0F : 0.0F;
1436 store_vector4(inst, machine, result);
1437 if (DEBUG_PROG) {
1438 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1439 result[0], result[1], result[2], result[3],
1440 a[0], a[1], a[2], a[3],
1441 b[0], b[1], b[2], b[3]);
1442 }
1443 }
1444 break;
1445 case OPCODE_SLT: /* set on less */
1446 {
1447 GLfloat a[4], b[4], result[4];
1448 fetch_vector4(&inst->SrcReg[0], machine, a);
1449 fetch_vector4(&inst->SrcReg[1], machine, b);
1450 result[0] = (a[0] < b[0]) ? 1.0F : 0.0F;
1451 result[1] = (a[1] < b[1]) ? 1.0F : 0.0F;
1452 result[2] = (a[2] < b[2]) ? 1.0F : 0.0F;
1453 result[3] = (a[3] < b[3]) ? 1.0F : 0.0F;
1454 store_vector4(inst, machine, result);
1455 if (DEBUG_PROG) {
1456 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1457 result[0], result[1], result[2], result[3],
1458 a[0], a[1], a[2], a[3],
1459 b[0], b[1], b[2], b[3]);
1460 }
1461 }
1462 break;
1463 case OPCODE_SNE: /* set on not equal */
1464 {
1465 GLfloat a[4], b[4], result[4];
1466 fetch_vector4(&inst->SrcReg[0], machine, a);
1467 fetch_vector4(&inst->SrcReg[1], machine, b);
1468 result[0] = (a[0] != b[0]) ? 1.0F : 0.0F;
1469 result[1] = (a[1] != b[1]) ? 1.0F : 0.0F;
1470 result[2] = (a[2] != b[2]) ? 1.0F : 0.0F;
1471 result[3] = (a[3] != b[3]) ? 1.0F : 0.0F;
1472 store_vector4(inst, machine, result);
1473 if (DEBUG_PROG) {
1474 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1475 result[0], result[1], result[2], result[3],
1476 a[0], a[1], a[2], a[3],
1477 b[0], b[1], b[2], b[3]);
1478 }
1479 }
1480 break;
1481 case OPCODE_SSG: /* set sign (-1, 0 or +1) */
1482 {
1483 GLfloat a[4], result[4];
1484 fetch_vector4(&inst->SrcReg[0], machine, a);
1485 result[0] = (GLfloat) ((a[0] > 0.0F) - (a[0] < 0.0F));
1486 result[1] = (GLfloat) ((a[1] > 0.0F) - (a[1] < 0.0F));
1487 result[2] = (GLfloat) ((a[2] > 0.0F) - (a[2] < 0.0F));
1488 result[3] = (GLfloat) ((a[3] > 0.0F) - (a[3] < 0.0F));
1489 store_vector4(inst, machine, result);
1490 }
1491 break;
1492 case OPCODE_STR: /* set true, operands ignored */
1493 {
1494 static const GLfloat result[4] = { 1.0F, 1.0F, 1.0F, 1.0F };
1495 store_vector4(inst, machine, result);
1496 }
1497 break;
1498 case OPCODE_SUB:
1499 {
1500 GLfloat a[4], b[4], result[4];
1501 fetch_vector4(&inst->SrcReg[0], machine, a);
1502 fetch_vector4(&inst->SrcReg[1], machine, b);
1503 result[0] = a[0] - b[0];
1504 result[1] = a[1] - b[1];
1505 result[2] = a[2] - b[2];
1506 result[3] = a[3] - b[3];
1507 store_vector4(inst, machine, result);
1508 if (DEBUG_PROG) {
1509 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1510 result[0], result[1], result[2], result[3],
1511 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
1512 }
1513 }
1514 break;
1515 case OPCODE_SWZ: /* extended swizzle */
1516 {
1517 const struct prog_src_register *source = &inst->SrcReg[0];
1518 const GLfloat *src = get_src_register_pointer(source, machine);
1519 GLfloat result[4];
1520 GLuint i;
1521 for (i = 0; i < 4; i++) {
1522 const GLuint swz = GET_SWZ(source->Swizzle, i);
1523 if (swz == SWIZZLE_ZERO)
1524 result[i] = 0.0;
1525 else if (swz == SWIZZLE_ONE)
1526 result[i] = 1.0;
1527 else {
1528 ASSERT(swz >= 0);
1529 ASSERT(swz <= 3);
1530 result[i] = src[swz];
1531 }
1532 if (source->Negate & (1 << i))
1533 result[i] = -result[i];
1534 }
1535 store_vector4(inst, machine, result);
1536 }
1537 break;
1538 case OPCODE_TEX: /* Both ARB and NV frag prog */
1539 /* Simple texel lookup */
1540 {
1541 GLfloat texcoord[4], color[4];
1542 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1543
1544 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1545
1546 if (DEBUG_PROG) {
1547 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1548 color[0], color[1], color[2], color[3],
1549 inst->TexSrcUnit,
1550 texcoord[0], texcoord[1], texcoord[2], texcoord[3]);
1551 }
1552 store_vector4(inst, machine, color);
1553 }
1554 break;
1555 case OPCODE_TXB: /* GL_ARB_fragment_program only */
1556 /* Texel lookup with LOD bias */
1557 {
1558 const GLuint unit = machine->Samplers[inst->TexSrcUnit];
1559 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
1560 GLfloat texcoord[4], color[4], lodBias;
1561
1562 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1563
1564 /* texcoord[3] is the bias to add to lambda */
1565 lodBias = texUnit->LodBias + texcoord[3];
1566 if (texUnit->_Current) {
1567 lodBias += texUnit->_Current->LodBias;
1568 }
1569
1570 fetch_texel(ctx, machine, inst, texcoord, lodBias, color);
1571
1572 store_vector4(inst, machine, color);
1573 }
1574 break;
1575 case OPCODE_TXD: /* GL_NV_fragment_program only */
1576 /* Texture lookup w/ partial derivatives for LOD */
1577 {
1578 GLfloat texcoord[4], dtdx[4], dtdy[4], color[4];
1579 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1580 fetch_vector4(&inst->SrcReg[1], machine, dtdx);
1581 fetch_vector4(&inst->SrcReg[2], machine, dtdy);
1582 machine->FetchTexelDeriv(ctx, texcoord, dtdx, dtdy,
1583 0.0, /* lodBias */
1584 inst->TexSrcUnit, color);
1585 store_vector4(inst, machine, color);
1586 }
1587 break;
1588 case OPCODE_TXP: /* GL_ARB_fragment_program only */
1589 /* Texture lookup w/ projective divide */
1590 {
1591 GLfloat texcoord[4], color[4];
1592
1593 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1594 /* Not so sure about this test - if texcoord[3] is
1595 * zero, we'd probably be fine except for an ASSERT in
1596 * IROUND_POS() which gets triggered by the inf values created.
1597 */
1598 if (texcoord[3] != 0.0) {
1599 texcoord[0] /= texcoord[3];
1600 texcoord[1] /= texcoord[3];
1601 texcoord[2] /= texcoord[3];
1602 }
1603
1604 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1605
1606 store_vector4(inst, machine, color);
1607 }
1608 break;
1609 case OPCODE_TXP_NV: /* GL_NV_fragment_program only */
1610 /* Texture lookup w/ projective divide, as above, but do not
1611 * do the divide by w if sampling from a cube map.
1612 */
1613 {
1614 GLfloat texcoord[4], color[4];
1615
1616 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1617 if (inst->TexSrcTarget != TEXTURE_CUBE_INDEX &&
1618 texcoord[3] != 0.0) {
1619 texcoord[0] /= texcoord[3];
1620 texcoord[1] /= texcoord[3];
1621 texcoord[2] /= texcoord[3];
1622 }
1623
1624 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1625
1626 store_vector4(inst, machine, color);
1627 }
1628 break;
1629 case OPCODE_TRUNC: /* truncate toward zero */
1630 {
1631 GLfloat a[4], result[4];
1632 fetch_vector4(&inst->SrcReg[0], machine, a);
1633 result[0] = (GLfloat) (GLint) a[0];
1634 result[1] = (GLfloat) (GLint) a[1];
1635 result[2] = (GLfloat) (GLint) a[2];
1636 result[3] = (GLfloat) (GLint) a[3];
1637 store_vector4(inst, machine, result);
1638 }
1639 break;
1640 case OPCODE_UP2H: /* unpack two 16-bit floats */
1641 {
1642 GLfloat a[4], result[4];
1643 const GLuint *rawBits = (const GLuint *) a;
1644 GLhalfNV hx, hy;
1645 fetch_vector1(&inst->SrcReg[0], machine, a);
1646 hx = rawBits[0] & 0xffff;
1647 hy = rawBits[0] >> 16;
1648 result[0] = result[2] = _mesa_half_to_float(hx);
1649 result[1] = result[3] = _mesa_half_to_float(hy);
1650 store_vector4(inst, machine, result);
1651 }
1652 break;
1653 case OPCODE_UP2US: /* unpack two GLushorts */
1654 {
1655 GLfloat a[4], result[4];
1656 const GLuint *rawBits = (const GLuint *) a;
1657 GLushort usx, usy;
1658 fetch_vector1(&inst->SrcReg[0], machine, a);
1659 usx = rawBits[0] & 0xffff;
1660 usy = rawBits[0] >> 16;
1661 result[0] = result[2] = usx * (1.0f / 65535.0f);
1662 result[1] = result[3] = usy * (1.0f / 65535.0f);
1663 store_vector4(inst, machine, result);
1664 }
1665 break;
1666 case OPCODE_UP4B: /* unpack four GLbytes */
1667 {
1668 GLfloat a[4], result[4];
1669 const GLuint *rawBits = (const GLuint *) a;
1670 fetch_vector1(&inst->SrcReg[0], machine, a);
1671 result[0] = (((rawBits[0] >> 0) & 0xff) - 128) / 127.0F;
1672 result[1] = (((rawBits[0] >> 8) & 0xff) - 128) / 127.0F;
1673 result[2] = (((rawBits[0] >> 16) & 0xff) - 128) / 127.0F;
1674 result[3] = (((rawBits[0] >> 24) & 0xff) - 128) / 127.0F;
1675 store_vector4(inst, machine, result);
1676 }
1677 break;
1678 case OPCODE_UP4UB: /* unpack four GLubytes */
1679 {
1680 GLfloat a[4], result[4];
1681 const GLuint *rawBits = (const GLuint *) a;
1682 fetch_vector1(&inst->SrcReg[0], machine, a);
1683 result[0] = ((rawBits[0] >> 0) & 0xff) / 255.0F;
1684 result[1] = ((rawBits[0] >> 8) & 0xff) / 255.0F;
1685 result[2] = ((rawBits[0] >> 16) & 0xff) / 255.0F;
1686 result[3] = ((rawBits[0] >> 24) & 0xff) / 255.0F;
1687 store_vector4(inst, machine, result);
1688 }
1689 break;
1690 case OPCODE_XOR: /* bitwise XOR */
1691 {
1692 GLuint a[4], b[4], result[4];
1693 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1694 fetch_vector4ui(&inst->SrcReg[1], machine, b);
1695 result[0] = a[0] ^ b[0];
1696 result[1] = a[1] ^ b[1];
1697 result[2] = a[2] ^ b[2];
1698 result[3] = a[3] ^ b[3];
1699 store_vector4ui(inst, machine, result);
1700 }
1701 break;
1702 case OPCODE_XPD: /* cross product */
1703 {
1704 GLfloat a[4], b[4], result[4];
1705 fetch_vector4(&inst->SrcReg[0], machine, a);
1706 fetch_vector4(&inst->SrcReg[1], machine, b);
1707 result[0] = a[1] * b[2] - a[2] * b[1];
1708 result[1] = a[2] * b[0] - a[0] * b[2];
1709 result[2] = a[0] * b[1] - a[1] * b[0];
1710 result[3] = 1.0;
1711 store_vector4(inst, machine, result);
1712 if (DEBUG_PROG) {
1713 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1714 result[0], result[1], result[2], result[3],
1715 a[0], a[1], a[2], b[0], b[1], b[2]);
1716 }
1717 }
1718 break;
1719 case OPCODE_X2D: /* 2-D matrix transform */
1720 {
1721 GLfloat a[4], b[4], c[4], result[4];
1722 fetch_vector4(&inst->SrcReg[0], machine, a);
1723 fetch_vector4(&inst->SrcReg[1], machine, b);
1724 fetch_vector4(&inst->SrcReg[2], machine, c);
1725 result[0] = a[0] + b[0] * c[0] + b[1] * c[1];
1726 result[1] = a[1] + b[0] * c[2] + b[1] * c[3];
1727 result[2] = a[2] + b[0] * c[0] + b[1] * c[1];
1728 result[3] = a[3] + b[0] * c[2] + b[1] * c[3];
1729 store_vector4(inst, machine, result);
1730 }
1731 break;
1732 case OPCODE_PRINT:
1733 {
1734 if (inst->SrcReg[0].File != -1) {
1735 GLfloat a[4];
1736 fetch_vector4(&inst->SrcReg[0], machine, a);
1737 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst->Data,
1738 a[0], a[1], a[2], a[3]);
1739 }
1740 else {
1741 _mesa_printf("%s\n", (const char *) inst->Data);
1742 }
1743 }
1744 break;
1745 case OPCODE_END:
1746 return GL_TRUE;
1747 default:
1748 _mesa_problem(ctx, "Bad opcode %d in _mesa_execute_program",
1749 inst->Opcode);
1750 return GL_TRUE; /* return value doesn't matter */
1751 }
1752
1753 numExec++;
1754 if (numExec > maxExec) {
1755 _mesa_problem(ctx, "Infinite loop detected in fragment program");
1756 return GL_TRUE;
1757 }
1758
1759 } /* for pc */
1760
1761 return GL_TRUE;
1762 }