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